
package com.yimixian.app.order;

import com.yimixian.app.R;
import com.yimixian.app.model.ValidatedOrder;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

public class DeliveryModeView extends LinearLayout {

    private List<DeliveryModeItemView> mDeliveryModeItemViewList = new ArrayList<>();

    public DeliveryModeView(Context context) {
        super(context);
        initDeliveryModeView();
    }

    public DeliveryModeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initDeliveryModeView();
    }

    public DeliveryModeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initDeliveryModeView();
    }

    private void initDeliveryModeView() {
        setOrientation(LinearLayout.VERTICAL);
    }

    public void bind(List<ValidatedOrder.DeliveryOptions> deliveryOptionsList, int deliveryId,
            final OnClickListener onClickListener) {
        mDeliveryModeItemViewList.clear();
        removeAllViews();
        boolean isEnable = deliveryOptionsList.size() > 0;
        for (int i = 0; i < deliveryOptionsList.size(); i++) {
            ValidatedOrder.DeliveryOptions deliveryOptions = deliveryOptionsList.get(i);

            final DeliveryModeItemView deliveryModeItemView = new DeliveryModeItemView(getContext());
            deliveryModeItemView.bind(deliveryOptions);
            if (isEnable) {
                if (deliveryOptions.getId() == deliveryId) {
                    deliveryModeItemView.setChecked(true);
                }
                deliveryModeItemView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        for (DeliveryModeItemView deliveryModeItem : mDeliveryModeItemViewList) {
                            deliveryModeItem.setChecked(false);
                        }
                        onClickListener.onClick(view);
                        deliveryModeItemView.setChecked(true);
                    }
                });
            } else {
                deliveryModeItemView.setEnabled(false);
                deliveryModeItemView.setCheckImageVisibility(View.GONE);
            }
            mDeliveryModeItemViewList.add(deliveryModeItemView);
            addView(deliveryModeItemView);
            if (i < deliveryOptionsList.size() - 1) {
                View divider = new View(getContext());
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT
                        , 1);
                int dividerMargin = getResources().getDimensionPixelOffset(
                        R.dimen.confirm_order_vertical_margin);
                layoutParams.setMargins(dividerMargin,
                        0, 0, 0);
                divider.setBackgroundColor(0xffd3d3d3);
                divider.setLayoutParams(layoutParams);
                addView(divider);
            }
        }
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        for (int i = 0; i < getChildCount(); i++) {
            getChildAt(i).setEnabled(enabled);
        }
    }
}
